package com.cwf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwf.common.entity.http.R;
import com.cwf.common.entity.page.PageResult;
import com.cwf.common.enums.BaseStateEnum;
import com.cwf.common.enums.IsDeleteEnum;
import com.cwf.common.enums.ResCodeEnum;
import com.cwf.common.exception.base.MyException;
import com.cwf.system.dao.RoleDao;
import com.cwf.system.entity.Role;
import com.cwf.system.service.RoleService;
import com.cwf.system.vo.RolePageListVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2023-02-15 13:06:59
 */


@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {


    @Autowired
    RoleDao roleDao;

    @Autowired
    RoleService roleService;


    @Override
    public PageResult<RolePageListVO> searchUser(Role role) {

            LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.like(role.getRoleName()!=null,Role::getRoleName,role.getRoleName())
                    .eq(role.getRoleId()!=null,Role::getRoleId,role.getRoleId())
                     .eq(Role::getIsDelete,IsDeleteEnum.NO);

            List<Role> roleList = roleService.list(lambdaQueryWrapper);



        PageInfo<Role> rolePageInfo = PageInfo.of(roleList);

        List<RolePageListVO> rolePageListVOS = new ArrayList<>();
        for (Role role1 : rolePageInfo.getList()) {
            RolePageListVO rolePageListVO = new RolePageListVO();
            BeanUtils.copyProperties(role1,rolePageListVO);
            rolePageListVOS.add(rolePageListVO);
        }
        PageResult<RolePageListVO> set = PageResult.set(rolePageInfo, rolePageListVOS);

        return set;
    }


    @Override
    public Role byIdGetRoleInfo(Long id) {

        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(Role::getRoleId,id)
                .eq(Role::getIsDelete,IsDeleteEnum.NO.getCode());
        Role role = roleDao.selectOne(lambdaQueryWrapper);

        if (role == null){
            throw new  MyException("查不到当前角色信息", ResCodeEnum.QUERY_ERR.getCode());
        }
        return role;
    }

    @Override
    public Boolean updateUser(Role role) {

        return roleService.updateById(role);
    }

    @Override
    public boolean saveUser(Role role) {

        return roleService.save(role);
    }

    @Override
    public Boolean deleteUser(Role role) {

//        role.setIsDelete(IsDeleteEnum.OK.getCode());
//
//        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//        lambdaQueryWrapper.eq(Role::getIsDelete,IsDeleteEnum.OK.getCode());



//        return roleService.updateById(update);
        return this.updateById(role);
    }
}

